package com.hdjk.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.util.ToastUtils;

/**
 * Created by sxy on 2017/11/8.
 */

public class EditTextDialog extends Dialog {
    private OnSureClickListener sureClickListener;
    private Context mContext;
    private String mTitle;
    private String mContent;
    private String mHintText;
    private Drawable mDrawable;
    public EditTextDialog(@NonNull Context context,String title,String content,Drawable drawable,String hintText, OnSureClickListener clickListener) {
        super(context);
        mContext=context;
        sureClickListener=clickListener;
        mTitle=title;
        mContent=content;
        mDrawable=drawable;
        mHintText=hintText;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_edittext);
        if (mDrawable==null){
            findViewById(R.id.iv_logo).setVisibility(View.GONE);
        }else {
            ((ImageView)findViewById(R.id.iv_logo)).setImageDrawable(mDrawable);
        }
        ((TextView)findViewById(R.id.tv_title)).setText(mTitle);
        ((EditText)findViewById(R.id.et_text)).setText(mContent);
        ((EditText)findViewById(R.id.et_text)).setHint(mHintText);
        ((EditText)findViewById(R.id.et_text)).setSelection(mContent.length());
        findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText text= (EditText) findViewById(R.id.et_text);
                if (TextUtils.isEmpty(text.getText().toString())){
                    ToastUtils.showShortToast(mContext,mHintText);
                }else {
                    sureClickListener.onSureClick(text.getText().toString());
                    dismiss();
                }
            }
        });
        findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    dismiss();
            }
        });
    }



    public interface OnSureClickListener{
        void onSureClick(String txt);
    }
}
